package hu.fic.weddingstorefinder.utils;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class Utils {
	
	public static void showDialog(String title, String message, String positiveButtonText, OnClickListener positiveButtonOnClickListener, String negativeButtonText, OnClickListener negativeButtonOnClickListener, Context context) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton(positiveButtonText, positiveButtonOnClickListener);
		if (negativeButtonText != null) {
			builder.setNegativeButton(negativeButtonText, negativeButtonOnClickListener);
		}
		builder.create().show();
	}

	public static void showInfoDialog(String title, String message, String buttonText, Context context) {
		Utils.showDialog(title, message, buttonText, null, null, null, context);
	}
	
	public static double getDistanceBetweenCoordinated(double fromLon, double fromLat, double toLon, double toLat) {
		float earthRadian = 6371000; //m
		double lat1 = Utils.degreesToRadians(fromLat); 
		double lat2 = Utils.degreesToRadians(toLat);
		double lon1 = Utils.degreesToRadians(fromLon);
		double lon2 = Utils.degreesToRadians(toLon);
		double retValue = Math.acos(Math.sin(lat1)*Math.sin(lat2) + Math.cos(lat1)*Math.cos(lat2)*Math.cos(lon2-lon1)) * earthRadian;
		return retValue;
	}
	
	public static double degreesToRadians(double degree) {
		return degree * Math.PI / 180;
	}

	public static String formatDistance(double meters) {
		String retValue = "km";
		if (meters < 1000) {
			retValue = (Math.round((double)meters/10) * 10) + " m";
		} else {
			long rounded = Math.round((double)meters/100);
			double distance = (double)rounded /(double)10;
			retValue = distance + " km";
		}
		return retValue;
	}
	
	public static String formatCurrency(double value, String currency) {
		Locale loc = new Locale("hu", "HU");
		DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(loc);
		unusualSymbols.setDecimalSeparator(',');
		unusualSymbols.setGroupingSeparator('.');
		String pattern = "###,###.###";
		DecimalFormat df = new DecimalFormat(pattern, unusualSymbols);
		String output = df.format(value) + " " + currency;
		return output;
	}
	
	public static boolean isOnline(Context context) {
	    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
	        return true;
	    }
	    return false;
	}
	
	

}
